/**
 * 
 */
package com.xbang.attendance.web.action.delivery.deliverylist;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.order.DeliveryDetails;
import com.xbang.attendance.entity.order.DeliveryOrder;
import com.xbang.attendance.security.support.SecurityUserHolder;
import com.xbang.attendance.service.DeliveryOrderService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class DeliveryManager extends SearchActionSupport {

	private static final long serialVersionUID = 7740221800667574687L;

	private final static Log logger = LogFactory.getLog(DeliveryManager.class);

	@Autowired
	private DeliveryOrderService deliveryOrderService;

	private DeliveryOrder deliveryOrder;

	private List<DeliveryDetails> deliveryDetails = new ArrayList<DeliveryDetails>();

	private Integer id;
	private String ids;
	
	
	@Action("/delivery/deliverydetails/${id}")
	public String detail() throws Exception {

		if (null != id) {
			deliveryOrder = (DeliveryOrder) deliveryOrderService.get(DeliveryOrder.class, id);

			String hqlSentence = "FROM DeliveryDetails deliverydetails WHERE 1 = 1 ";

			searchComponent.addSearchElement("eq.deliverydetails.deliveryOrder.id", id);
			Result result = deliveryOrderService.searchByPage(hqlSentence, searchComponent, page);

			deliveryDetails = result.getItems();
			page = result.getPage();

		}

		return "detail";
	}

	@Action("/delivery/list/export")
	public String export() throws Exception {
		String exportFile =null;
		String webPath =ServletActionContext.getRequest().getRealPath("//download//order//delivery");
		HttpServletRequest request = ServletActionContext.getRequest();
		String urlPath = request.getRequestURL().toString();
		String contextPath = request.getContextPath();
		String contextURL = urlPath.split(contextPath)[0] + contextPath;
		exportFile = contextURL + "/download/order/delivery/" + deliveryOrderService.genericExcelFile(webPath, null);
		return "r:" + exportFile;
	}
	
	@Action("/delivery/list/exportdelivery")
	public String exportDelivery() throws Exception {
		String exportFile =null;
		String webPath =ServletActionContext.getRequest().getRealPath("//download//order//delivery");
		String templatePath =ServletActionContext.getRequest().getRealPath("//template//送货清单.xls");
		HttpServletRequest request = ServletActionContext.getRequest();
		String urlPath = request.getRequestURL().toString();
		String contextPath = request.getContextPath();
		String contextURL = urlPath.split(contextPath)[0] + contextPath;
		
		String[] idss = ids.split(",");
	//	System.out.println(idss);
		
		exportFile = contextURL + "/download/order/delivery/" + deliveryOrderService.genericExcelFile(webPath, templatePath, idss);
		return "r:" + exportFile;
	}
	
	@Action("/delivery/list/exportdeliverydetail")
	public String exportDeliveryDetail() throws Exception {
		String exportFile =null;
		String webPath =ServletActionContext.getRequest().getRealPath("//download//order//delivery");
		//String templatePath =ServletActionContext.getRequest().getRealPath("//template//送货清单.xls");
		HttpServletRequest request = ServletActionContext.getRequest();
		String urlPath = request.getRequestURL().toString();
		String contextPath = request.getContextPath();
		String contextURL = urlPath.split(contextPath)[0] + contextPath;
		
		String[] idss = ids.split(",");
	//	System.out.println(idss);
		
		exportFile = contextURL + "/download/order/delivery/" + deliveryOrderService.genericExcelFile(webPath, idss);
		return "r:" + exportFile;
	}
	@Action("/delivery/list/${id}/edit")
	public String edit() throws Exception {

		if (null != id) {
			deliveryOrder = (DeliveryOrder) deliveryOrderService.get(DeliveryOrder.class, id);
		}

		return "edit";
	}

	@Action("/delivery/list/${id}/view")
	public String view() throws Exception {
		return "view";
	}

	@Action("/delivery/list/save")
	public String save() throws Exception {
		// DeliveryOrder deliveryOrder =
		// updateDeliveryStatus
		
		if (deliveryOrder != null) {
			deliveryOrderService.updateDeliveryStatus(deliveryOrder, deliveryOrder.getDeliveryStatus(), new Date(), SecurityUserHolder.getCurrentUser()
					.getUsername());
		}

		return "r:/delivery/deliverylist/index/"+currPageId;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the deliveryOrder
	 */
	public DeliveryOrder getDeliveryOrder() {
		return deliveryOrder;
	}

	/**
	 * @param deliveryOrder
	 *            the deliveryOrder to set
	 */
	public void setDeliveryOrder(DeliveryOrder deliveryOrder) {
		this.deliveryOrder = deliveryOrder;
	}

	/**
	 * @return the deliveryDetails
	 */
	public List<DeliveryDetails> getDeliveryDetails() {
		return deliveryDetails;
	}

	/**
	 * @param deliveryDetails
	 *            the deliveryDetails to set
	 */
	public void setDeliveryDetails(List<DeliveryDetails> deliveryDetails) {
		this.deliveryDetails = deliveryDetails;
	}

	/**
	 * @param deliveryOrderService
	 *            the deliveryOrderService to set
	 */
	public void setDeliveryOrderService(DeliveryOrderService deliveryOrderService) {
		this.deliveryOrderService = deliveryOrderService;
	}

	@Override
	public void resetCustomerSearchCriteria() {

	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	
}
